class ArticlesController < ApplicationController
  before_filter :check_editor_role, :except => [:index, :show]
  layout "newsarticles"
   uses_tiny_mce(:options => {:theme => 'advanced',
                           :browsers => %w{msie gecko},
                           :theme_advanced_toolbar_location => "top",
                           :theme_advanced_toolbar_align => "left",
                           :theme_advanced_resizing => true,
                           :theme_advanced_resize_horizontal => false,
                           :paste_auto_cleanup_on_paste => true,
                           :theme_advanced_buttons1 => %w{formatselect fontselect fontsizeselect bold italic underline strikethrough separator justifyleft justifycenter justifyright indent outdent separator bullist numlist forecolor backcolor separator link unlink image media undo redo},
                           :theme_advanced_buttons2 =>%w{cut,copy,paste,pastetext,pasteword|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,media,anchor,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor},
                           :theme_advanced_buttons3 =>%w{tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen,insertlayer},
                           :editor_selector => "tiny_mce",                           	
                           :theme_advanced_statusbar_location=>"bottom",
                           :plugins => %w{contextmenu paste preview media}},
              :content_css=> "css/content.css",
              :only => [:new, :edit, :show, :index])
  def index
    if params[:category_id]
      @articles_pages, @articles = paginate(:articles, 
        :include => :user, 
        :order => 'published_at DESC',
        :conditions => "category_id=#{params[:category_id].to_i} AND published=true")
    else
      @articles = Article.find_all_by_published(true)
      @articles_pages, @articles = paginate(:articles, 
        :include => :user,
        :order => 'published_at DESC', 
        :conditions => "published = true")        
    end
    respond_to do |wants|
      wants.html
      wants.xml  { render :xml => @articles.to_xml }
      wants.rss  { render :action => 'rss.rxml', :layout => false }
      wants.atom { render :action => 'atom.rxml', :layout => false }
    end
  end

  def show
    if is_logged_in? && @logged_in_user.has_role?('Editor')
      @article = Article.find(params[:id])
    else
      @article = Article.find_by_id_and_published(params[:id], true)
    end
    respond_to do |wants|
      wants.html
      wants.xml { render :xml => @article.to_xml }
    end
  end
  
  def new  
    @article = Article.new

        @test = ""
        @abc = "<br/><img src="
        
        @images=(params[:img].nil?)? []:params[:img].split("==");
        
    @images.each do |image|
    	image = @abc+image+">"
    	@test = @test + image
    end
    @article.body=params[:selection] unless params[:selection].nil?
    
    @article.body=@test+@article.body
  end
  


  
  def create


    @article = Article.create(params[:article])

    
    @logged_in_user.articles << @article
    
    
    @images=(params[:images].nil?)? []:params[:images].split("==");
    p @images[0]
    @images.each do |image|

    	@imageurl = Imageurl.new
      @imageurl.article_id = @article.id
      @imageurl.simgurl = image
    	@article.imageurls<< @imageurl

    end    
    respond_to do |wants|
      wants.html { redirect_to admin_articles_url }
      wants.xml  { render :xml => @article.to_xml }
    end
  end
  
  def edit
    @article = Article.find(params[:id])
  end
  
  def update
    @article = Article.find(params[:id])
    @article.update_attributes(params[:article])
    respond_to do |wants|
      wants.html { redirect_to admin_articles_url }
      wants.xml  { render :xml => @article.to_xml }
    end
  end
  
  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    respond_to do |wants|
      wants.html { redirect_to admin_articles_url }
      wants.xml  { render :nothing => true }
    end
  end

  def admin
    @articles_pages, @articles = paginate(:articles, :order => 'published_at DESC')
  end
end
